********************************************************************************
**  TITLE:      us2008_naes_contact_networks                                  **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       naes08-phone-nat-rcs-data-compact                             **
**  DATE:	    October 2022 						                          **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2008_naes_contact_networks", replace

* Open the dataset

use "naes08-phone-nat-rcs-data-compact.dta", clear


***************************
** INDEPENDENT VARIABLES **
***************************

* Time (presidential election)

gen surveydate=date(string(date_c,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20081104

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Time (Democratic primary election)

gen ddate = 20080828

gen demdate=date(string(ddate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY demdate

gen dtime = demdate - surveydate

* Time (Republican primary election)

gen rdate = 20080904

gen repdate=date(string(rdate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY repdate

gen rtime = repdate - surveydate

* Gender

gen male = wa01_c

recode male (2=0)

* Age

gen age = wa02_c

recode age (998=.)
recode age (999=.)

replace age = . if age < 17

* Education

gen education = wa03_c

recode education (998=.)
recode education (999=.)

* Interest for election

gen interest = ka01

recode interest (998=.)
recode interest (999=.)

replace interest = 5 - interest

* Party identification

gen pidstatus_whole = ma01

recode pidstatus_whole (3=2) // It appears that Democrats were mistakenly coded as Independents in the original dataset
recode pidstatus_whole (4=3)
recode pidstatus_whole (998=.)
recode pidstatus_whole (999=.)

recode pidstatus_whole 2=3 3=2

label define pidstatus 1 "Loser PID" 2 "No PID/Other" 3 "Winner PID" 
label values pidstatus_whole pidstatus

* Party identification scale

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & ma02 == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & ma02 == 2
replace pidscale_whole = 3 if ma03 == 1
replace pidscale_whole = 4 if pidstatus_whole == 2 & ma03 == 3 | pidstatus_whole == 2 & ma03 == 998
replace pidscale_whole = 5 if ma03 == 2
replace pidscale_whole = 6 if pidstatus_whole == 3 & ma02 == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & ma02 == 1

label define pidscale 1 "Strong loser PID" 2 "Moderate loser PID" 3 "Weak loser PID" 4 "No PID/Other" 5 "Weak winner PID" 6 "Moderate winner PID" 7 "Strong winner PID" 
label values pidscale_whole pidscale

* Evaluation of McCain

gen mccainrating = aam01

recode mccainrating (998=.)
recode mccainrating (999=.)

* Evaluation of Giuliani

gen giulianirating = aag01

recode giulianirating (998=.)
recode giulianirating (999=.)

* Evaluation of Romney

gen romneyrating = aar01

recode romneyrating (998=.)
recode romneyrating (999=.)

* Evaluation of Huckabee

gen huckabeerating = aah01

recode huckabeerating (998=.)
recode huckabeerating (999=.)

* Evaluation of Thompson

gen thompsonrating = aat01

recode thompsonrating (998=.)
recode thompsonrating (999=.)

* Republican primary preference

gen repprefA = mccainrating 
gen repprefB = giulianirating  
gen repprefC = romneyrating 
gen repprefD = huckabeerating 
gen repprefE = thompsonrating 

gen reppref_complete = 1 if repprefA!=. & repprefB!=. & repprefC!=. & repprefD!=. & repprefE!=.

gen reppreference = 3 if repprefA >= repprefB & repprefA >= repprefC & repprefA >= repprefD & repprefA >= repprefE & reppref_complete == 1
replace reppreference = 2 if repprefA == repprefB & repprefA == repprefC & repprefA == repprefD & repprefA == repprefE & reppref_complete == 1
replace reppreference = 1 if reppreference!=2 & reppreference!=3 & reppref_complete == 1

replace reppreference = . if reppreference == 3 & repprefA == repprefB & reppref_complete == 1
replace reppreference = . if reppreference == 3 & repprefA == repprefC & reppref_complete == 1
replace reppreference = . if reppreference == 3 & repprefA == repprefD & reppref_complete == 1
replace reppreference = . if reppreference == 3 & repprefA == repprefE & reppref_complete == 1

label define reppreference 1 "Loser Republican preference" 2 "No preference" 3 "Winner Republican preference"
label values reppreference reppreference

* Evaluation of Obama

gen obamarating = abo01

recode obamarating (998=.)
recode obamarating (999=.)

* Evaluation of Clinton

gen clintonrating = abc01

recode clintonrating (998=.)
recode clintonrating (999=.)

* Evaluation of Edwards

gen edwardsrating = abe01

recode edwardsrating (998=.)
recode edwardsrating (999=.)

* Democratic primary preference

gen demprefA = obamarating 
gen demprefB = clintonrating  
gen demprefC = edwardsrating 

gen dempref_complete = 1 if demprefA!=. & demprefB!=. & demprefC!=.

gen dempreference = 3 if demprefA >= demprefB & demprefA >= demprefC & dempref_complete == 1
replace dempreference = 2 if demprefA == demprefB & demprefA == demprefC & dempref_complete == 1
replace dempreference = 1 if dempreference!=2 & dempreference!=3 & dempref_complete == 1

replace dempreference = . if dempreference == 3 & demprefA == demprefB & dempref_complete == 1
replace dempreference = . if dempreference == 3 & demprefA == demprefC & dempref_complete == 1

label define dempreference 1 "Loser Democratic preference" 2 "No preference" 3 "Winner Democratic preference"
label values dempreference dempreference

* National TV news attentiveness

gen tv = eb02_c

recode tv (998=.)
recode tv (999=.)

recode tv (.=-99)

* Radio news attentiveness

gen radio = ec01_c

recode radio (998=.)
recode radio (999=.)

recode radio (.=-99)

* Press news attentiveness

gen press = ed01_c

recode press (998=.)
recode press (999=.)

recode press (.=-99)

* Internet news attentiveness

gen internet = ee02_c

recode internet (998=.)
recode internet (999=.)

replace internet = 0 if wg01_c == 2

recode internet (.=-99)

* News attentiveness index

egen news = rowmax(tv radio press internet)

recode news (-99=.)

* Political discussion

gen discussion = kb01_c

recode discussion (998=.)
recode discussion (999=.)

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Political disagreement

gen disagreement = kb02_c

recode disagreement (998=.)
recode disagreement (999=.)

replace disagreement = 5 - disagreement

replace disagreement = 0 if discussion == 0

gen disagreement_3pts = disagreement
replace disagreement_3pts = 0 if disagreement == 1
replace disagreement_3pts = 1 if disagreement == 2
replace disagreement_3pts = 2 if disagreement == 3
replace disagreement_3pts = 2 if disagreement == 4


***************************************
** REPUBLICAN PRIMARIES EXPECTATIONS **
***************************************

* Generate variable identifying incomplete answers

gen giulianiprimaries = naa01
gen huckabeeprimaries = naa02
gen mccainprimaries = naa03
gen romneyprimaries = naa04
gen thompsonprimaries = naa05

gen complete_repprimaries_1 = 1 if giulianiprimaries!=. & huckabeeprimaries!=. & mccainprimaries!=. & romneyprimaries!=. & thompsonprimaries!=. & electiondate <= date("20080121","YMD")
gen complete_repprimaries_2 = 1 if giulianiprimaries!=. & huckabeeprimaries!=. & mccainprimaries!=. & romneyprimaries!=. & electiondate <= date("20080129","YMD")
gen complete_repprimaries_3 = 1 if huckabeeprimaries!=. & mccainprimaries!=. & romneyprimaries!=. & electiondate > date("20080206","YMD") & electiondate <= date("20040304","YMD")
gen complete_repprimaries_4 = 1 if huckabeeprimaries!=. & mccainprimaries!=. & electiondate > date("20080304","YMD")

* Generate value of 1 if the expectation for one candidate is higher or equal to the expectations for at least one other candidate

gen repprimariesA = 1 if giulianiprimaries >= huckabeeprimaries & giulianiprimaries >= mccainprimaries & giulianiprimaries >= romneyprimaries & giulianiprimaries >= thompsonprimaries & complete_repprimaries_1 == 1
gen repprimariesB = 1 if huckabeeprimaries >= giulianiprimaries & huckabeeprimaries >= mccainprimaries & huckabeeprimaries >= romneyprimaries & huckabeeprimaries >= thompsonprimaries & complete_repprimaries_1 == 1
gen repprimariesC = 1 if mccainprimaries >= giulianiprimaries & mccainprimaries >= huckabeeprimaries & mccainprimaries >= romneyprimaries & mccainprimaries >= thompsonprimaries & complete_repprimaries_1 == 1
gen repprimariesD = 1 if romneyprimaries >= giulianiprimaries & romneyprimaries >= huckabeeprimaries & romneyprimaries >= mccainprimaries & romneyprimaries >= thompsonprimaries & complete_repprimaries_1 == 1
gen repprimariesE = 1 if thompsonprimaries >= giulianiprimaries & thompsonprimaries >= huckabeeprimaries & thompsonprimaries >= mccainprimaries & thompsonprimaries >= romneyprimaries & complete_repprimaries_1 == 1
gen repprimariesOTH = 1 if giulianiprimaries == 0 & huckabeeprimaries == 0 & mccainprimaries == 0 & romneyprimaries == 0 & thompsonprimaries == 0 & complete_repprimaries_1 == 1

replace repprimariesA = . if repprimariesOTH == 1 & complete_repprimaries_1 == 1
replace repprimariesB = . if repprimariesOTH == 1 & complete_repprimaries_1 == 1
replace repprimariesC = . if repprimariesOTH == 1 & complete_repprimaries_1 == 1
replace repprimariesD = . if repprimariesOTH == 1 & complete_repprimaries_1 == 1
replace repprimariesE = . if repprimariesOTH == 1 & complete_repprimaries_1 == 1

replace repprimariesA = 1 if giulianiprimaries >= huckabeeprimaries & giulianiprimaries >= mccainprimaries & giulianiprimaries >= romneyprimaries & complete_repprimaries_2 == 1
replace repprimariesB = 1 if huckabeeprimaries >= giulianiprimaries & huckabeeprimaries >= mccainprimaries & huckabeeprimaries >= romneyprimaries & complete_repprimaries_2 == 1
replace repprimariesC = 1 if mccainprimaries >= giulianiprimaries & mccainprimaries >= huckabeeprimaries & mccainprimaries >= romneyprimaries & complete_repprimaries_2 == 1
replace repprimariesD = 1 if romneyprimaries >= giulianiprimaries & romneyprimaries >= huckabeeprimaries & romneyprimaries >= mccainprimaries & complete_repprimaries_2 == 1
replace repprimariesOTH = 1 if giulianiprimaries == 0 & huckabeeprimaries == 0 & mccainprimaries == 0 & romneyprimaries == 0 & complete_repprimaries_2 == 1

replace repprimariesA = . if repprimariesOTH == 1 & complete_repprimaries_2 == 1
replace repprimariesB = . if repprimariesOTH == 1 & complete_repprimaries_2 == 1
replace repprimariesC = . if repprimariesOTH == 1 & complete_repprimaries_2 == 1
replace repprimariesD = . if repprimariesOTH == 1 & complete_repprimaries_2 == 1

replace repprimariesB = 1 if huckabeeprimaries >= mccainprimaries & huckabeeprimaries >= romneyprimaries & complete_repprimaries_3 == 1
replace repprimariesC = 1 if mccainprimaries >= huckabeeprimaries & mccainprimaries >= romneyprimaries & complete_repprimaries_3 == 1
replace repprimariesD = 1 if romneyprimaries >= huckabeeprimaries & romneyprimaries >= mccainprimaries & complete_repprimaries_3 == 1
replace repprimariesOTH = 1 if huckabeeprimaries == 0 & mccainprimaries == 0 & romneyprimaries == 0 & complete_repprimaries_3 == 1

replace repprimariesB = . if repprimariesOTH == 1 & complete_repprimaries_3 == 1
replace repprimariesC = . if repprimariesOTH == 1 & complete_repprimaries_3 == 1
replace repprimariesD = . if repprimariesOTH == 1 & complete_repprimaries_3 == 1

replace repprimariesB = 1 if huckabeeprimaries >= mccainprimaries & complete_repprimaries_4 == 1
replace repprimariesC = 1 if mccainprimaries >= huckabeeprimaries & complete_repprimaries_4 == 1
replace repprimariesOTH = 1 if huckabeeprimaries == 0 & mccainprimaries == 0 & complete_repprimaries_4 == 1

replace repprimariesB = . if repprimariesOTH == 1 & complete_repprimaries_4 == 1
replace repprimariesC = . if repprimariesOTH == 1 & complete_repprimaries_4 == 1

* Generate variable for ambiguous forecasts

gen ambiguous_repprimaries = .

replace ambiguous_repprimaries = 99 if repprimariesA == repprimariesB & repprimariesA!=. | repprimariesA == repprimariesC & repprimariesA!=. | repprimariesA == repprimariesD & repprimariesA!=. | repprimariesA == repprimariesE & repprimariesA!=.
replace ambiguous_repprimaries = 99 if repprimariesB == repprimariesA & repprimariesB!=. | repprimariesB == repprimariesC & repprimariesB!=. | repprimariesB == repprimariesD & repprimariesB!=. | repprimariesB == repprimariesE & repprimariesB!=.
replace ambiguous_repprimaries = 99 if repprimariesC == repprimariesA & repprimariesC!=. | repprimariesC == repprimariesB & repprimariesC!=. | repprimariesC == repprimariesD & repprimariesC!=. | repprimariesC == repprimariesE & repprimariesC!=.
replace ambiguous_repprimaries = 99 if repprimariesD == repprimariesA & repprimariesD!=. | repprimariesD == repprimariesB & repprimariesD!=. | repprimariesD == repprimariesC & repprimariesD!=. | repprimariesD == repprimariesE & repprimariesD!=.
replace ambiguous_repprimaries = 99 if repprimariesE == repprimariesA & repprimariesE!=. | repprimariesE == repprimariesB & repprimariesE!=. | repprimariesE == repprimariesC & repprimariesE!=. | repprimariesE == repprimariesD & repprimariesE!=.

* Generate value of 0 for lower expectations

replace repprimariesA = 0 if repprimariesA!=1 & repprimariesA!=. & complete_repprimaries_1 == 1
replace repprimariesB = 0 if repprimariesB!=1 & repprimariesB!=. & complete_repprimaries_1 == 1
replace repprimariesC = 0 if repprimariesC!=1 & repprimariesC!=. & complete_repprimaries_1 == 1
replace repprimariesD = 0 if repprimariesD!=1 & repprimariesD!=. & complete_repprimaries_1 == 1
replace repprimariesE = 0 if repprimariesE!=1 & repprimariesE!=. & complete_repprimaries_1 == 1
replace repprimariesOTH = 0 if repprimariesOTH!=1 & repprimariesOTH!=. & complete_repprimaries_1 == 1

replace repprimariesA = 0 if repprimariesA!=1 & repprimariesA!=. & complete_repprimaries_2 == 1
replace repprimariesB = 0 if repprimariesB!=1 & repprimariesB!=. & complete_repprimaries_2 == 1
replace repprimariesC = 0 if repprimariesC!=1 & repprimariesC!=. & complete_repprimaries_2 == 1
replace repprimariesD = 0 if repprimariesD!=1 & repprimariesD!=. & complete_repprimaries_2 == 1
replace repprimariesOTH = 0 if repprimariesOTH!=1 & repprimariesOTH!=. & complete_repprimaries_2 == 1

replace repprimariesB = 0 if repprimariesB!=1 & repprimariesB!=. & complete_repprimaries_3 == 1
replace repprimariesC = 0 if repprimariesC!=1 & repprimariesC!=. & complete_repprimaries_3 == 1
replace repprimariesD = 0 if repprimariesD!=1 & repprimariesD!=. & complete_repprimaries_3 == 1
replace repprimariesOTH = 0 if repprimariesOTH!=1 & repprimariesOTH!=. & complete_repprimaries_3 == 1

replace repprimariesB = 0 if repprimariesB!=1 & repprimariesB!=. & complete_repprimaries_4 == 1
replace repprimariesC = 0 if repprimariesC!=1 & repprimariesC!=. & complete_repprimaries_4 == 1
replace repprimariesOTH = 0 if repprimariesOTH!=1 & repprimariesOTH!=. & complete_repprimaries_4 == 1

* Forecasts (1 = Giuliani, 2 = Huckabee, 3 = Mccain, 3 = Romney, 5 = Thompson, 88 = other, 99 = ambiguous)

gen forecast_repprimaries = .

replace forecast_repprimaries = 1 if repprimariesA == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 2 if repprimariesB == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 3 if repprimariesC == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 4 if repprimariesD == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 5 if repprimariesE == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 88 if repprimariesOTH == 1 & complete_repprimaries_1 == 1
replace forecast_repprimaries = 99 if ambiguous_repprimaries == 1 & complete_repprimaries_1 == 1

replace forecast_repprimaries = 1 if repprimariesA == 1 & complete_repprimaries_2 == 1
replace forecast_repprimaries = 2 if repprimariesB == 1 & complete_repprimaries_2 == 1
replace forecast_repprimaries = 3 if repprimariesC == 1 & complete_repprimaries_2 == 1
replace forecast_repprimaries = 4 if repprimariesD == 1 & complete_repprimaries_2 == 1
replace forecast_repprimaries = 88 if repprimariesOTH == 1 & complete_repprimaries_2 == 1
replace forecast_repprimaries = 99 if ambiguous_repprimaries == 1 & complete_repprimaries_2 == 1

replace forecast_repprimaries = 2 if repprimariesB == 1 & complete_repprimaries_3 == 1
replace forecast_repprimaries = 3 if repprimariesC == 1 & complete_repprimaries_3 == 1
replace forecast_repprimaries = 4 if repprimariesD == 1 & complete_repprimaries_3 == 1
replace forecast_repprimaries = 88 if repprimariesOTH == 1 & complete_repprimaries_3 == 1
replace forecast_repprimaries = 99 if ambiguous_repprimaries == 1 & complete_repprimaries_3 == 1

replace forecast_repprimaries = 2 if repprimariesB == 1 & complete_repprimaries_4 == 1
replace forecast_repprimaries = 3 if repprimariesC == 1 & complete_repprimaries_4 == 1
replace forecast_repprimaries = 88 if repprimariesOTH == 1 & complete_repprimaries_4 == 1
replace forecast_repprimaries = 99 if ambiguous_repprimaries == 1 & complete_repprimaries_4 == 1

* Identify correct and ambiguous forecasts

gen correct_repprimaries = 0 if forecast_repprimaries!=3 & forecast_repprimaries!=. & complete_repprimaries_1 == 1
replace correct_repprimaries = 1 if forecast_repprimaries == 3 & complete_repprimaries_1 == 1

replace correct_repprimaries = 0 if forecast_repprimaries!=3 & forecast_repprimaries!=. & complete_repprimaries_2 == 1
replace correct_repprimaries = 1 if forecast_repprimaries == 3 & complete_repprimaries_2 == 1

replace correct_repprimaries = 0 if forecast_repprimaries!=3 & forecast_repprimaries!=. & complete_repprimaries_3 == 1
replace correct_repprimaries = 1 if forecast_repprimaries == 3 & complete_repprimaries_3 == 1

replace correct_repprimaries = 0 if forecast_repprimaries!=3 & forecast_repprimaries!=. & complete_repprimaries_4 == 1
replace correct_repprimaries = 1 if forecast_repprimaries == 3 & complete_repprimaries_4 == 1

replace correct_repprimaries = 99 if forecast_repprimaries == 99 & repprimariesC == 1 & complete_repprimaries_1 == 1

replace correct_repprimaries = 99 if forecast_repprimaries == 99 & repprimariesC == 1 & complete_repprimaries_2 == 1

replace correct_repprimaries = 99 if forecast_repprimaries == 99 & repprimariesC == 1 & complete_repprimaries_3 == 1

replace correct_repprimaries = 99 if forecast_repprimaries == 99 & repprimariesC == 1 & complete_repprimaries_4 == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_repprimaries_d = correct_repprimaries

replace correct_repprimaries_d = 0 if correct_repprimaries_d == 99
replace correct_repprimaries_d = 1 if correct_repprimaries_d == 1


***************************************
** DEMOCRATIC PRIMARIES EXPECTATIONS **
***************************************

* Generate variable identifying incomplete answers

gen clintonprimaries = naa06_c
gen edwardsprimaries = naa07_c
gen obamaprimaries = naa08_c

gen complete_demprimaries_1 = 1 if clintonprimaries!=. & edwardsprimaries!=. & obamaprimaries!=. & electiondate <= date("20080129","YMD")
gen complete_demprimaries_2 = 1 if clintonprimaries!=. & obamaprimaries!=. & electiondate > date("20080608","YMD")

* Generate value of 1 if the expectation for one candidate is higher or equal to the expectations for at least one other candidate

gen demprimariesA = 1 if clintonprimaries >= edwardsprimaries & clintonprimaries >= obamaprimaries & complete_demprimaries_1 == 1
gen demprimariesB = 1 if edwardsprimaries >= clintonprimaries & edwardsprimaries >= obamaprimaries & complete_demprimaries_1 == 1
gen demprimariesC = 1 if obamaprimaries >= clintonprimaries & obamaprimaries >= edwardsprimaries & complete_demprimaries_1 == 1
gen demprimariesOTH = 1 if clintonprimaries == 0 & edwardsprimaries == 0 & obamaprimaries == 0 & complete_demprimaries_1 == 1

replace demprimariesA = . if demprimariesOTH == 1 & complete_demprimaries_1 == 1
replace demprimariesB = . if demprimariesOTH == 1 & complete_demprimaries_1 == 1
replace demprimariesC = . if demprimariesOTH == 1 & complete_demprimaries_1 == 1

replace demprimariesA = 1 if clintonprimaries >= obamaprimaries & complete_demprimaries_2 == 1
replace demprimariesC = 1 if obamaprimaries >= clintonprimaries & complete_demprimaries_2 == 1
replace demprimariesOTH = 1 if clintonprimaries == 0 & obamaprimaries == 0 & complete_demprimaries_2 == 1

replace demprimariesA = . if demprimariesOTH == 1 & complete_demprimaries_2 == 1
replace demprimariesC = . if demprimariesOTH == 1 & complete_demprimaries_2 == 1

* Generate variable for ambiguous forecasts

gen ambiguous_demprimaries = .

replace ambiguous_demprimaries = 99 if demprimariesA == demprimariesB & demprimariesA!=. | demprimariesA == demprimariesC & demprimariesA!=.
replace ambiguous_demprimaries = 99 if demprimariesB == demprimariesA & demprimariesB!=. | demprimariesB == demprimariesC & demprimariesB!=.
replace ambiguous_demprimaries = 99 if demprimariesC == demprimariesA & demprimariesC!=. | demprimariesC == demprimariesB & demprimariesC!=.

* Generate value of 0 for lower expectations

replace demprimariesA = 0 if demprimariesA!=1 & demprimariesA!=. & complete_demprimaries_1 == 1
replace demprimariesB = 0 if demprimariesB!=1 & demprimariesB!=. & complete_demprimaries_1 == 1
replace demprimariesC = 0 if demprimariesC!=1 & demprimariesC!=. & complete_demprimaries_1 == 1
replace demprimariesOTH = 0 if demprimariesOTH!=1 & demprimariesOTH!=. & complete_demprimaries_1 == 1

replace demprimariesA = 0 if demprimariesA!=1 & demprimariesA!=. & complete_demprimaries_2 == 1
replace demprimariesC = 0 if demprimariesC!=1 & demprimariesC!=. & complete_demprimaries_2 == 1
replace demprimariesOTH = 0 if demprimariesOTH!=1 & demprimariesOTH!=. & complete_demprimaries_2 == 1

* Forecasts (1 = Clinton, 2 = Edwards, 3 = Obama, 88 = other, 99 = ambiguous)

gen forecast_demprimaries = .

replace forecast_demprimaries = 1 if demprimariesA == 1 & complete_demprimaries_1 == 1
replace forecast_demprimaries = 2 if demprimariesB == 1 & complete_demprimaries_1 == 1
replace forecast_demprimaries = 3 if demprimariesC == 1 & complete_demprimaries_1 == 1
replace forecast_demprimaries = 88 if demprimariesOTH == 1 & complete_demprimaries_1 == 1
replace forecast_demprimaries = 99 if ambiguous_demprimaries == 1 & complete_demprimaries_1 == 1

replace forecast_demprimaries = 1 if demprimariesA == 1 & complete_demprimaries_2 == 1
replace forecast_demprimaries = 3 if demprimariesC == 1 & complete_demprimaries_2 == 1
replace forecast_demprimaries = 88 if demprimariesOTH == 1 & complete_demprimaries_2 == 1
replace forecast_demprimaries = 99 if ambiguous_demprimaries == 1 & complete_demprimaries_2 == 1

* Identify correct and ambiguous forecasts

gen correct_demprimaries = 0 if forecast_demprimaries!=3 & forecast_demprimaries!=. & complete_demprimaries_1 == 1
replace correct_demprimaries = 1 if forecast_demprimaries == 3 & complete_demprimaries_1 == 1

replace correct_demprimaries = 0 if forecast_demprimaries!=3 & forecast_demprimaries!=. & complete_demprimaries_2 == 1
replace correct_demprimaries = 1 if forecast_demprimaries == 3 & complete_demprimaries_2 == 1

replace correct_demprimaries = 99 if forecast_demprimaries == 99 & demprimariesC == 1 & complete_demprimaries_1 == 1

replace correct_demprimaries = 99 if forecast_demprimaries == 99 & demprimariesC == 1 & complete_demprimaries_2 == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_demprimaries_d = correct_demprimaries

replace correct_demprimaries_d = 0 if correct_demprimaries_d == 99
replace correct_demprimaries_d = 1 if correct_demprimaries_d == 1


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen mccainchances = nba03_c
gen obamachances = nba07_c

recode mccainchances (101=.)
recode mccainchances (999=.)
recode mccainchances (999=.)

recode obamachances (101=.)
recode obamachances (999=.)
recode obamachances (999=.)

gen mccainwin = 1 if mccainchances > 50
gen obamawin = 1 if obamachances > 50

gen dk = 1 if mccainchances == 50
replace dk = 1 if obamachances == 50
replace dk = 1 if nba03_c == 998
replace dk = 1 if nba07_c == 998

gen forecast_whole = 1 if mccainwin == 1
replace forecast_whole = 2 if obamawin == 1
replace forecast_whole = 99 if dk == 1

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=2 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 2


**********
** SAVE **
**********

save "us2008_naes.dta", replace


*****************************************
** MAIN ANALYSES: REPUBLICAN PRIMARIES **
*****************************************

* Regression analysis and percentage of correct and incorrect forecasts (Democratic primaries)

logistic correct_repprimaries_d discussion mccainrating interest news age i.male education rtime
estimates table, star(.05 .01 .001)

tab correct_repprimaries_d if e(sample) == 1

set seed 1234

bsample 2000 if e(sample) == 1

logistic correct_repprimaries_d discussion mccainrating interest news age i.male education rtime
estimates table, star(.05 .01 .001)

tab correct_repprimaries_d if e(sample) == 1


**********************************************
** SUMMARY STATISTICS: REPUBLICAN PRIMARIES **
**********************************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_repprimaries_d "Republican nominee (DV)"
label variable correct_demprimaries_d "Democratic nominee (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable pidscale_whole "National PID"
label variable mccainrating "McCain rating"
label variable obamarating "Obama rating"
label variable interest "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable rtime "Time of interview"

estpost tabstat correct_repprimaries_d discussion mccainrating interest news age male education rtime if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2008_naes_summary_repprimaries.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2008 Republican presidential primaries (NAES), national level") replace


*****************************************
** MAIN ANALYSES: DEMOCRATIC PRIMARIES **
*****************************************

* Regression analysis and percentage of correct and incorrect forecasts (Democratic primaries)

logistic correct_demprimaries_d discussion disagreement obamarating interest news age i.male education dtime
estimates table, star(.05 .01 .001)

tab correct_demprimaries_d if e(sample) == 1


**********************************************
** SUMMARY STATISTICS: DEMOCRATIC PRIMARIES **
**********************************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_repprimaries_d "Republican nominee (DV)"
label variable correct_demprimaries_d "Democratic nominee (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable pidscale_whole "National PID"
label variable mccainrating "McCain rating"
label variable obamarating "Obama rating"
label variable interest "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable dtime "Time of interview"

estpost tabstat correct_demprimaries_d discussion disagreement obamarating interest news age male education dtime if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2008_naes_summary_demprimaries.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2008 Democratic presidential primaries (NAES), national level") replace


******************************************
** MAIN ANALYSES: PRESIDENTIAL ELECTION **
******************************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion disagreement pidscale_whole interest news age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************************
** SUMMARY STATISTICS: PRESIDENTIAL ELECTION **
***********************************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_repprimaries_d "Republican nominee (DV)"
label variable correct_demprimaries_d "Democratic nominee (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable pidscale_whole "National PID"
label variable mccainrating "McCain rating"
label variable obamarating "Obama rating"
label variable interest "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

estpost tabstat correct_whole_d discussion disagreement pidscale_whole interest news age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2008_naes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2008 American presidential election (NAES), national level") replace

eststo clear

log close